package com.example.finalapp;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Toast;

import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParsePush;
import com.parse.ParseQuery;
import com.parse.ParseUser;

public class HomeActivity extends SplitActionBarActivity {
	WifiManager wifi;
	boolean isEnabled= false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);

		ParseUser user = ParseUser.getCurrentUser();
		
		//--- send push to all apartment roommates ---//
		try {
			JSONObject data = new JSONObject("{\"action\": \"com.example.UPDATE_IS_HOME\", "
					+ "\"SSID\": "+ ParseUser.getCurrentUser().get("SSID") +"}");
			ParsePush push = new ParsePush();
			push.setChannel((String)user.get("Apartment"));
			push.setData(data);
			push.sendInBackground();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		ParseQuery<ParseUser> query = ParseUser.getQuery();
		query.whereEqualTo("Apartment", ParseUser.getCurrentUser().get("Apartment"));
		query.whereEqualTo("IsHome", true);
		query.findInBackground(new FindCallback<ParseUser>() {
			public void done(List<ParseUser> objects, ParseException e) {
				if (e == null) 
				{
					StringBuilder str= new StringBuilder();
					for(int i=0; i < objects.size(); i++)
					{
						str.append((String)objects.get(i).get("Name") + " ");
					}
					Toast.makeText(HomeActivity.this, "in Home: "+str.toString(), Toast.LENGTH_LONG).show();
				} 
				else 
				{
					// Something went wrong.
				}
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		super.onCreateOptionsMenu(menu);
		getMenuInflater().inflate(R.menu.home_continue, menu);
		return true;
	}

}
